﻿using System;
using System.Collections.Generic;
using PerCiGal.Common;
using PerCiGal.Types;

namespace PerCiGal.WebInfo.Movie.themoviedb.org
{
    public class Engine : WebInfoInterface
    {
        private const string Key = "df26c33ba1e6b4940727aa15d777ab97";

        /// <summary>
        /// The Movie.getInfo method is used to retrieve specific information about a movie. Things like overview, release date, cast data, genre's, YouTube trailer link, etc...
        /// </summary>
        /// <param name="id">The ID of the TMDb movie you are searching for.</param>
        /// <param name="language"> IETF Language Tag, e.g. en-US</param>
        /// <returns></returns>
        public Types.MOVIE_WEBDETAILS GetMovieDetails(string id, Types.Language language)
        {
            const string baseAddress = "http://api.themoviedb.org/2.1/Movie.getInfo/{0}/xml/{1}/{2}";

            string url = string.Format(baseAddress, language.ToString(), Key, id);

            string page = FunctionWeb.GetPage(url);

            var result = (Movie.getInfo.OpenSearchDescription)FunctionXML.StringToXML(typeof(Movie.getInfo.OpenSearchDescription), page);

            System.Diagnostics.Debug.Write(FunctionXML.ObjectToXMLString(typeof(Movie.getInfo.OpenSearchDescription), result));

            if( (result.movies != null) && (result.movies.Length >0))
            {
                var movieDetails = new Types.MOVIE_WEBDETAILS();

                //movieDetails.DISTRIBUTION;
                movieDetails.DURATION = string.IsNullOrEmpty(result.movies[0].runtime) ? (short?)null : short.Parse(result.movies[0].runtime);
                //movieDetails.GENRES;
                movieDetails.MAIN_TITLE = result.movies[0].name;
                movieDetails.MINOR_TITLE = result.movies[0].alternative_name;
                //movieDetails.NATIONS;
                movieDetails.OFFICIAL_SITE = result.movies[0].homepage;
                movieDetails.ORIGINAL_TITLE = result.movies[0].original_name;
                movieDetails.PLOT = result.movies[0].overview;

                foreach (var image in result.movies[0].images)
                {
                    if (image.size == "cover")
                    {
                        movieDetails.POSTER = image.url;
                        break;
                    }

                    movieDetails.POSTER = image.url;
                }

                //movieDetails.PRODUCTION;
                //movieDetails.RATE = result.movies[0].rating;
                movieDetails.RELEASE_DATE = string.IsNullOrEmpty(result.movies[0].released) ? (DateTime?)null : DateTime.Parse(result.movies[0].released);
                movieDetails.RELEASE_YEAR = string.IsNullOrEmpty(result.movies[0].released) ? (short?)null : (short)DateTime.Parse(result.movies[0].released).Year;
                movieDetails.SOURCE = "themoviedb.org";
                movieDetails.SOURCE_LINK = result.movies[0].url;
                movieDetails.TRAILER_PATH = result.movies[0].trailer;

                foreach (var cast in result.movies[0].cast)
                {
                    var movieCast = new MOVIE_CAST();

                    movieCast.CHARACTER_NAME = cast.character;
                    movieCast.NAME = cast.name;
                    movieCast.ROLE_DESCRIPTION = cast.job;

                    movieDetails.CAST.Add(movieCast);
                }

                return movieDetails;            
            }
            else return null;

        }

        /// <summary>
        /// The Movie.search method is the easiest and quickest way to search for a movie. It is a mandatory method in order to get the movie id to pass to (as an example) the Movie.getInfo method.
        /// </summary>
        /// <param name="title">The title of the movie you are searching for: you can add the year of the movie to your search string to narrow your results.</param>
        /// <param name="language"> IETF Language Tag, e.g. en-US</param>
        /// <returns></returns>
        public Types.MOVIE_WEBLIST GetMovieList(string title, Types.Language language)
        {
            const string baseAddress = "http://api.themoviedb.org/2.1/Movie.search/{0}/xml/{1}/{2}";

            string url = string.Format(baseAddress, language.ToString(), Key, title);

            string page = FunctionWeb.GetPage(url);

            var result = (Movie.search.OpenSearchDescription)FunctionXML.StringToXML(typeof(Movie.search.OpenSearchDescription), page);

            var movieWeblist = new Types.MOVIE_WEBLIST {Items = new List<Types.MOVIE_WEBLISTDETAILS>()};

            foreach (var movie in result.movies)
            {
                if (movie.translated == "true")
                {
                    var movieWebdetails = new Types.MOVIE_WEBLISTDETAILS();

                    movieWebdetails.COD_MOVIE = movie.id;
                    movieWebdetails.MAIN_TITLE = movie.name;

                    if (!string.IsNullOrEmpty(movie.released))
                    {
                        movieWebdetails.RELEASE_YEAR = (short) DateTime.Parse(movie.released).Year;
                    }

                    movieWebdetails.ORIGINAL_TITLE = movie.original_name;

                    movieWebdetails.OVERVIEW = movie.overview;

                    foreach (var image in movie.images)
                    {
                        if (image.size == "cover")
                        {
                            movieWebdetails.POSTER = image.url;
                            break;
                        }

                        movieWebdetails.POSTER = image.url;
                    }

                    movieWeblist.Items.Add(movieWebdetails);
                }
            }

            return movieWeblist;
        }
    }

 

}
